/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-31
 * Time: 17:33
 */
//ListNode
class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}
public class MyLinkedList {
    public ListNode head;

    //打印链表
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字k
    public boolean findK(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int getLength(){
        ListNode cur = this.head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    public ListNode findIndex(int index){
        ListNode cur = this.head;
        while (index - 1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addPos(int pos, int data){
        if (pos < 0 || pos > getLength()){
            System.out.println("插入位置不合法！");
            return;
        }
        if (pos == 0){
            addFirst(data);
            return;
        }
        if (pos == getLength()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(pos);
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现的关键字为key的节点
    public void remove(int key){
        if (this.head == null){
            System.out.println("单链表为空，不能删除！");
            return;
        }
        if (this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println("未找到该节点！");
    }

    //删除所有值为key的节点
    public ListNode removeAll(int key){
        if (this.head == null){
            System.out.println("单链表为空，不能删除！");
            return null;
        }

        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            } else{
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key){
            this.head = this.head.next;
        }
        return this.head;
    }

    //清空单链表
    public void clear(){
        //this.head = null;
        while (this.head != null){
            ListNode cur = this.head.next;
            this.head.next = null;
            this.head = cur;
        }
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        if (this.head == null){
            return null;
        }
        ListNode prev = null;
        ListNode cur = this.head;
        ListNode curNext = this.head;
        while (cur != null){
            curNext = curNext.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }

    //返回中间节点 - 快慢指针
    public ListNode middleNode(ListNode head) {
        if (this.head == null){
            return null;
        }

        ListNode fast = this.head;
        ListNode slow = this.head;

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }

    //倒数第K个节点
    public ListNode FindKthToTail(ListNode head,int k) {
        if (this.head == null || k < 0){
            return null;
        }

        ListNode fast = this.head;
        ListNode slow = this.head;

        while (fast != null && fast.next != null){
            if (k - 1 != 0){
                fast = fast.next;
                k--;
            } else{
                fast = fast.next;
                slow = slow.next;
            }
        }
        if (k - 1 != 0){
            return null;
        }
        return slow;
    }

    //合并有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null && list2 == null){
            return null;
        }
        ListNode p1 = list1;
        ListNode p2 = list2;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (p1 != null && p2 != null){
            if (p1.val < p2.val){
                tmp.next = p1;
                p1 = p1.next;
                tmp = tmp.next;
            } else {
                tmp.next = p2;
                p2 = p2.next;
                tmp = tmp.next;
            }
        }
        if (p1 != null){
            tmp.next = p1;
        } else{
            tmp.next = p2;
        }
        return newHead.next;
    }

    //链表分割
    public ListNode partition(ListNode pHead, int x) {
        if (pHead == null){
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        while (cur != null){
            if (cur.val < x){
                if (bs == null){
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null){
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null){
            return as;
        }
        be.next = as;
        if (ae != null){
            ae.next = null;
        }
        return bs;
    }

    //删除重复节点
    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null){
            return null;
        }
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        ListNode cur = this.head;

        while (cur != null){
            if (cur.next != null && cur.val == cur.next.val){
                while (cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            } else{
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;
        return newHead.next;
    }

    //回文链表
    public boolean chkPalindrome(ListNode A) {
        if (A == null){
            return false;
        }

        ListNode fast = A;
        ListNode slow = A;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        ListNode cur = A;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            cur = cur.next;
            slow.next = tmp;
            tmp = slow;
            slow = cur;
        }

        while (slow != null){
            if (slow.val != tmp.val){
               return false;
            }
            slow = slow.next;
            tmp = tmp.next;
        }
        return true;
    }

    //公共节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null){
            return null;
        }

        ListNode last = headB;
        while (last.next != null){
            last = last.next;
        }
        last.next = headB;

        ListNode fast = headA;
        ListNode slow = headA;

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                fast = headA;
                while (fast != slow){
                    fast = fast.next;
                    slow = slow.next;
                }
                last.next = null;
                return slow;
            }
        }
        last.next = null;
        return null;
    }

    //公共节点II
    public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
        if (headA == null || headB == null){
            return null;
        }
        ListNode p1 = headA;
        ListNode p2 = headB;
        while (p1 != p2){
            p1 = p1 == null ? headB : p1.next;
            p2 = p2 == null ? headA : p2.next;
        }
        return p1;
    }

    public ListNode getIntersectionNode3(ListNode headA, ListNode headB) {
        if (headA == null || headB == null){
            return null;
        }

        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while (pl != null){
            lenA++;
            pl = pl.next;
        }
        pl = headA;
        while (ps != null){
            lenB++;
            ps = ps.next;
        }
        ps = headB;
        int len = lenA - lenB;
        if (len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }

        while (len != 0){
            pl = pl.next;
            len--;
        }
        while (pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }

    //判断链表是否有环
    public boolean hasCycle(ListNode head) {
        if (head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }

    //判断链表是否有环并返回节点
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                fast = head;
                while (fast != slow){
                    fast = fast.next;
                    slow = slow.next;
                }
                return slow;
            }
        }
        return null;
    }
}
